/**
 * 
 */
package mainServer;

/**
 * @author kin
 *
 */

import interaction.Protocol;
import interaction.ProtocolType;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

public class GuiServer extends JFrame implements ActionListener 
{
	private static final long serialVersionUID = 1L;
	private JSpinner edit_Port = new JSpinner(new SpinnerNumberModel(13000,1025,14000,1));
	private JButton ButtonStart = new JButton("Стартовать БД");
	private JButton ButtonKill = new JButton("Убить БД ");
	private JComboBox BoxPr = new JComboBox();
	private ServerFactory myFactory = new ServerFactory();
	private IServer myServer;
	
	/**
	 * 
	 */
	public GuiServer() {
		super("Поисковик - сервер");
		
		BorderLayout layout = new BorderLayout();
		setLayout(layout);
	    JPanel panel_param = new JPanel();
	    panel_param.add(new JLabel("  Порт сервера : "));
	    panel_param.add(edit_Port);
	    
	    JPanel panel_param1 = new JPanel();
	    
	    for(Protocol prot: Protocol.m_protocols)
	        BoxPr.addItem(prot.m_name);
	    panel_param1.add(new JLabel("  Протокол подключения: "));
	    panel_param1.add(BoxPr);
	    
	    
	    JPanel panel_button = new JPanel();
	    panel_button.add(ButtonStart);
	    panel_button.add(ButtonKill);
	    ButtonKill.setVisible(false);
	    
	    add(panel_param, BorderLayout.NORTH);
	    add(panel_param1, BorderLayout.CENTER);
	    add(panel_button, BorderLayout.SOUTH);
	    	    
	    setSize(400,200);
	    setResizable(false);
	    
	    ButtonStart.setActionCommand("start");
	    ButtonStart.addActionListener(this);
	    ButtonKill.setActionCommand("kill");
	    ButtonKill.addActionListener(this);
	    	    
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		
		GuiServer serverwindow = new GuiServer();
		serverwindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		serverwindow.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent command) 
	  {
		String str = command.getActionCommand();
		 if(str.equalsIgnoreCase("start"))
		    {
		    	System.out.println("Запуск БД");
		    	ProtocolType type = Protocol.m_protocolMap.get(BoxPr.getSelectedItem()).m_protocol;
		        int port = ((Integer)(edit_Port.getValue())).intValue();
		        
		       
		        System.out.println("Запуск сервера...");
		        myServer = myFactory.makeServer(type, port);
		        
		        if(myServer != null)
		        {
		          boolean result = myServer.start();
		          
		          if(result == true)
		          {
		           ButtonStart.setVisible(false);
		           ButtonKill.setVisible(true);
		        	  System.out.println("Сервер запущен");
		          }
		          else
		            System.out.println("Невозможно запустить");
		        }
		        else
		          System.out.println("Невозможно запустить");
		    }
		    else if(str.equalsIgnoreCase("kill"))
		    {
		    	System.out.println("Остановка БД");
		    	boolean result = myServer.stop();
		    	 if(result == true)
		    	    {
		    		   ButtonStart.setVisible(true);
			           ButtonKill.setVisible(false);
		    	      System.out.println("Сервер остановлен");
		    	    }
		    	    else
		    	      System.out.println("Невозможно остановить сервер");
		    }
	  }

}
